package com.sbox.album;

import com.sbox.album.model.AlbumContext;
import com.sbox.album.model.AlbumInfo;
import com.sbox.album.model.PictureInfo;
import com.sbox.util.AlbumLog;
import com.sbox.util.PictureUtils;
import com.sbox.widget.TitleBar;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class PictureActivity extends Activity {
	private TitleBar mTitleBar = null;
	private ImageView mImage = null;
	private LinearLayout mBtnGroup = null;
	private AlbumInfo mAlbum = null;
	private PictureInfo mPicture = null; 
	private boolean mFullScreen = false;	
	private Bitmap mBitmap = null;
	
	private View.OnClickListener mPicListener = new View.OnClickListener() {		
		@Override
		public void onClick(View v) {
			mFullScreen = !mFullScreen;
			
			if( mFullScreen ) {
				showFullscreen(true);
			}
			else {
				showFullscreen(false);
			}
		}
	};
	
	private PicGestureListener mPicGestureListener = new PicGestureListener();
	GestureDetector mGestureDetector = null;
	
	private View.OnTouchListener mTouchListener = new View.OnTouchListener() {		
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if( mGestureDetector == null )
				mGestureDetector = new GestureDetector(PictureActivity.this, mPicGestureListener);
			mGestureDetector.onTouchEvent(event);			
			return false;
		}
	};
	
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_pic);
        
        mTitleBar = (TitleBar)findViewById(R.id.pic_title_bar);
        mImage = (ImageView)findViewById(R.id.pic_image);
        mImage.setOnClickListener(mPicListener);
        mImage.setOnTouchListener(mTouchListener);
        mImage.setLongClickable(true);
        
        mBtnGroup = (LinearLayout)findViewById(R.id.pic_btn_group);
        
        mAlbum = AlbumContext.getAlbum();
        mPicture = AlbumContext.getPicture();
        
        startAutoHideTask();
    }
	
	@Override
	public void onResume() {
		super.onResume();
		
		setTitle();
		showPicture();
	}
	
	@Override
	public void onStop() {
		super.onStop();
		
		if( mBitmap != null ) {
			mBitmap.recycle();
			mBitmap = null;
		}
	}
	
	private void setTitle() {
		String title = null;
		
		if( mAlbum == null )
			return;
		
		title = mAlbum.getName() + "(";
		title += String.valueOf(AlbumContext.getPicIndex()+1);
		title += "/";
		title += String.valueOf(mAlbum.getPictureCount());
		title += ")";
		
		mTitleBar.getTextView().setText(title);
	}
	
	private void showPicture() {
		if( mPicture == null )
			return;
		
		if( mBitmap != null )
			mBitmap.recycle();
		
		mBitmap = PictureUtils.createPicture(mPicture.getFileName());
		if( mBitmap == null ){
			AlbumLog.error("load bitmap failed, file=" + mPicture.getFileName());
			return;
		}
		mImage.setImageBitmap( mBitmap );
	}
	
	private void moveToNext() {
		if( mAlbum == null ) {
			AlbumLog.error("mAlbum == null");
			return;
		}
		
		int index = AlbumContext.getPicIndex();
		int count = mAlbum.getPictureCount();
		
		if( index >= count-1 ) {
			return;
		}
		
		index++;
		mPicture = mAlbum.getPicture(index);
		if( mPicture == null ) {
			return;
		}
		
		AlbumContext.setPicIndex(index);
		AlbumContext.setPicture(mPicture);
		
		setTitle();
		showPicture();
	}
	
	private void moveToPrev() {
		if( mAlbum == null ) {
			return;
		}
		
		int index = AlbumContext.getPicIndex();
		//int count = mAlbum.getPictureCount();
		
		if( index <= 0 ) {
			return;
		}
		
		index--;
		mPicture = mAlbum.getPicture(index);
		if( mPicture == null ) {
			return;
		}
		
		AlbumContext.setPicIndex(index);
		AlbumContext.setPicture(mPicture);
		
		setTitle();
		showPicture();
	}	
	
	private void startAutoHideTask() {
		AutoHideTask hide = new AutoHideTask();
		hide.execute(null);
	}
	
	private void showFullscreen(boolean full) {
		if( full ) {
			PictureActivity.this.mTitleBar.setVisibility(View.GONE);
			PictureActivity.this.mBtnGroup.setVisibility(View.GONE);
		} else {
			PictureActivity.this.mTitleBar.setVisibility(View.VISIBLE);
			PictureActivity.this.mBtnGroup.setVisibility(View.VISIBLE);
		}
	}
	
	public class PicGestureListener extends SimpleOnGestureListener {
		
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY) {
			if( distanceX < 0 ) {
				if( Math.abs(distanceX) > Math.abs(distanceY)*2) {			
					moveToNext();
				}
			}
			else {
				if( Math.abs(distanceX) > Math.abs(distanceY)*2 )
					moveToPrev();
			}
			
			return false;
        }

	}
	
	public class AutoHideTask extends AsyncTask<Integer, Integer, Integer> {
		protected Integer doInBackground(Integer... params) {
			SystemClock.sleep(5000);
			
			return 0;
		}
		
		protected void onPostExecute(Integer result) {
			PictureActivity.this.showFullscreen(true);
	    }
	};
}
